import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Logger;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class DisplayCommand implements Command {

	
	public ArrayList<Task> tasks;
	public AddParser addParser;
	public Storage storage;
	public History history;
	public Logger logger;
	private boolean isError = false;
	private Boolean dueDateExist = false;
	String data;
	String errorMessage;
	String output;
	DateTime deadline,startDate,endDate;
	
	private static final int BASEINDEX = 1;
	private static final String DD_MM_YYYY = "dd/MM/yyyy";
	private static final String DESIRED_TASK = "%d. %s\n";
	private static final String DESIRED_TASK_BY_DATE = "%d. %s by %s\n";
	private static final String DESIRED_TIMED_TASK_BY_DATE = "%d. %s from %s to %s\n";
	private static final String INVALID_COMMAND = "Command is invalid\n";
	private static final String COMMAND_DISPLAY = "display";
	

	// messages for display
	private static final String DISPLAY_EMPTY_TASK_LIST = "There is no tasks recorded.\n";

	public DisplayCommand(ArrayList<Task> tasks, Storage storage, History history,
			Logger logger) {
		this.tasks = tasks;
		this.addParser = new AddParser();
		this.storage = storage;
		this.history = history;
		this.logger = logger;
		errorMessage = "";
	}

	@Override
	public void acceptInput(String data) {
		// TODO Auto-generated method stub
		this.data = data;

	}

	@Override
	public void execute() {
		// TODO Auto-generated method stub
		String allTasks = DISPLAY_EMPTY_TASK_LIST;
		StringBuilder obtainAllTasks = new StringBuilder();
		DateTimeFormatter formatter = DateTimeFormat.forPattern(DD_MM_YYYY);
		data = data.trim();
		if (!data.equals(COMMAND_DISPLAY)) {
			output = INVALID_COMMAND;
			isError = true;
		}

		for (Task task : tasks) {
			dueDateExist = false;
			deadline = startDate = endDate = null;
			if (task.getDeadline() != null) {
				deadline = task.getDeadline();
				dueDateExist = true;
			} else if (task.getEnd() != null) {
				startDate = task.getStart();
				endDate = task.getEnd();
				dueDateExist = true;
			}
			if (dueDateExist) {
				if(deadline!=null){
				obtainAllTasks.append(String.format(DESIRED_TASK_BY_DATE,
						tasks.indexOf(task) + BASEINDEX, task.getContent(),formatter.print(deadline)));
				}else if (endDate!=null){
					obtainAllTasks.append(String.format(DESIRED_TIMED_TASK_BY_DATE,
							tasks.indexOf(task) + BASEINDEX, task.getContent(),formatter.print(startDate),formatter.print(endDate)));
				}
			} else {
				obtainAllTasks.append(String.format(DESIRED_TASK,
						tasks.indexOf(task) + BASEINDEX, task.getContent()));
			}
		}
		allTasks = obtainAllTasks.toString();
		allTasks.trim();
		if (tasks.isEmpty()) {
			output = DISPLAY_EMPTY_TASK_LIST;
		} else {
			output = allTasks;
		}

	}

	@Override
	public String save() throws IOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String outputToUser() {
		// TODO Auto-generated method stub
		return output;
	}

	@Override
	public boolean isError() {
		// TODO Auto-generated method stub
		return isError;
	}

}
